/**
 *
 */
package com.ybkj.daijia.server.service.impl;


import com.ybkj.daijia.common.Pager;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.page.Pageable;
import com.ybkj.daijia.page.impl.PageImpl;
import com.ybkj.daijia.page.impl.PageRequest;
import com.ybkj.daijia.server.mapper.OrderMapper;
import com.ybkj.daijia.server.mapper.OrderStatusMapper;
import com.ybkj.daijia.server.model.OrderStatus;
import com.ybkj.daijia.server.service.OrderStatusService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * @author Shine
 *
 */
@Service
public class DefaultOrderStatusService implements OrderStatusService {

    @Autowired
    private OrderStatusMapper orderStatusMapper;

    @Autowired
    private OrderMapper orderMapper;


    public Page<OrderStatus> queryAll(final Long orderId) {

        Pageable pageable = new PageRequest(Pager.getPage(), Pager.getLimit());

        List<OrderStatus> content = orderStatusMapper
            .selectByPage(orderId, Pager.getOffset(), Pager.getLimit());
        long total = orderStatusMapper.countByPage(orderId);

        return new PageImpl<OrderStatus>(content, pageable, total);
    }

    @Transactional(rollbackFor = Exception.class)
    public OrderStatus save(OrderStatus orderStatus) {

        orderStatusMapper.insertRecord(orderStatus);

        return orderStatus;
    }

    @Override
    public OrderStatus findOne(Long id) {

        return orderStatusMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<OrderStatus> findByOrder(Long orderId) {

        return orderStatusMapper.selectByOrder(orderId);

    }

    @Override
    public OrderStatus findLastChufa(long orderId) {

        return orderStatusMapper.findLastChufa(orderId);
    }

    @Override
    public List<OrderStatus> findAllRefuse() {
        List<OrderStatus> orderStatuss = orderStatusMapper
            .selectByCompanyAndTypeWithParams(null, OrderStatus.StatusType.refuse, null, null,
                null);
        if (!CollectionUtils.isEmpty(orderStatuss)) {
            for (OrderStatus orderStatus : orderStatuss) {
                orderStatus.setOrder(orderMapper.selectByPrimaryKey(orderStatus.getOrderId()));
            }
        }
        return orderStatuss;
    }
}